package com.inxedu.os.edu.controller.article;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inxedu.os.edu.pojo.EduArticle;
import com.inxedu.os.edu.pojo.EduArticleContent;
import com.inxedu.os.edu.pojo.QueryArticle;
import com.inxedu.os.edu.service.article.AdminArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 后台文章Controller
 */
@Controller
@RequestMapping("/admin/article")
@Api(value = "后台文章管理接口",tags = "后台文章管理接口")
public class AdminArticleController {
    @Autowired
    private AdminArticleService adminArticleService;

    /**
     * 模糊查看文章列表
     * @param request
     * @param queryArticle
     * @param page
     * @return
     */
    @RequestMapping(value = "/showlist",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "模糊查看文章列表",notes = "模糊查看文章列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "queryArticle",value = "模糊查看条件类",dataType = "QueryArticle.java",paramType = "query"),
    @ApiImplicitParam(name = "page",value = "分页信息",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "queryKey",value = "查询信息",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "查询信息",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "orderby",value = "排序条件",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "count",value = "查询数据量",dataType = "int",paramType = "query"),})
    public ModelAndView lookArticleList(HttpServletRequest request, @ModelAttribute("queryArticle") QueryArticle queryArticle, @RequestParam(defaultValue = "1") int page){
        ModelAndView model = new ModelAndView();

        model.setViewName("view/inxedu/admin/article/article-list");
        //设置分页信息
        PageHelper.startPage(page,10);
        List<EduArticle> eduArticles = adminArticleService.lookArticle(queryArticle);

        PageInfo<EduArticle> info = new PageInfo<EduArticle>(eduArticles);
        request.setAttribute("page",info);
        model.addObject("articleList",eduArticles);
//        model.addObject("page",page);
        return model;
    }

    /**
     * 获取操作完成返回原有页面
     */
    private String getRequestUri(HttpServletRequest request) {
        Object uri = request.getSession().getAttribute("requestUri");
        if (uri == null) {
            return "redirect:/admin/article/showlist";
        } else {
            return "redirect:" + uri.toString();
        }
    }

    /**
     * 执行修改文章
     * @param request
     * @param article
     * @param articleContent
     * @return
     */
    @RequestMapping(value = "/updatearticle",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "修改文章",notes = "修改文章")
    @ApiImplicitParams({@ApiImplicitParam(name = "article",value = "文章信息",paramType = "query"),
            @ApiImplicitParam(name = "articleId",value = "文章id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "title",value = "文章标题",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "summary",value = "文章摘要",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "keyWord",value = "文章关键字",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "imageUrl",value = "文章图片URL",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "source",value = "文章来源",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "author",value = "文章作者",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "createTime",value = "文章创建时间",dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "publishTime",value = "文章发布时间",dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "link",value = "文章访问链接",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "文章类型",dataType = "byte",paramType = "query"),
            @ApiImplicitParam(name = "clickNum",value = "文章点击量",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "praiseCount",value = "点赞数量",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "commentNum",value = "评论数",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "sort",value = "排序值",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "articleContent",value = "文章内容表",paramType = "query"),
            @ApiImplicitParam(name = "articleId",value = "文章ID",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "content",value = "文章内容",dataType = "String",paramType = "query")})
    public ModelAndView updateArticle(HttpServletRequest request, @ModelAttribute("article") EduArticle article, @ModelAttribute("articleContent") EduArticleContent articleContent){

        ModelAndView model = new ModelAndView();
        Object uurl = request.getSession().getAttribute("uurl");
        article.setImageUrl((String) uurl);
        adminArticleService.updateArticle(article);
        articleContent.setArticleId(article.getArticleId());
        adminArticleService.updateArticleContent(articleContent);
        model.setViewName(this.getRequestUri(request));
        return model;

    }

    /**
     * 返回当前点击修改文章的内容和信息
     * @param request
     * @param articleId
     * @return
     */
    @RequestMapping(value = "/initupdate/{articleId}",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "返回当前点击修改文章的内容和信息",notes = "返回当前点击修改文章的内容和信息")
    @ApiImplicitParam(name = "articleId",value = "文章Id",dataType = "int",paramType = "path")
    public ModelAndView initUpdateArticle(HttpServletRequest request, @PathVariable("articleId") int articleId){
        ModelAndView model = new ModelAndView();
        model.setViewName("view/inxedu/admin/article/update-article");
        EduArticle eduArticle = adminArticleService.findArticleById(articleId);
        String articleCont = adminArticleService.findArticleContentByArticleId(articleId);
        model.addObject("article",eduArticle);
        model.addObject("content",articleCont);
        return model;
    }

    /**
     *删除文章
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "删除文章",notes = "删除文章")
    public ModelAndView delete(HttpServletRequest request){
        ModelAndView model = new ModelAndView();
        model.setViewName(this.getRequestUri(request));
        String[] articelIds = request.getParameterValues("articelId");
        if(articelIds != null || articelIds.length > 0){
            adminArticleService.deleteArticleByIds(articelIds);
        }
        return model;
    }

    /**
     * 去添加文章页面
     * @return
     */
    @RequestMapping(value = "/initcreate",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "添加文章页面",notes = "添加文章页面")
    public ModelAndView toAddArticel(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("view/inxedu/admin/article/add-article");
        return modelAndView;
    }

    /**
     * 执行添加文章
     * @param article
     * @param articleContent
     * @return
     */
    @RequestMapping(value = "/addarticle",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "执行添加文章",notes = "执行添加文章")
    @ApiImplicitParams({@ApiImplicitParam(name = "article",value = "文章信息",paramType = "query"),
            @ApiImplicitParam(name = "articleId",value = "文章id",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "title",value = "文章标题",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "summary",value = "文章摘要",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "keyWord",value = "文章关键字",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "imageUrl",value = "文章图片URL",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "source",value = "文章来源",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "author",value = "文章作者",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "createTime",value = "文章创建时间",dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "publishTime",value = "文章发布时间",dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "link",value = "文章访问链接",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "文章类型",dataType = "byte",paramType = "query"),
            @ApiImplicitParam(name = "clickNum",value = "文章点击量",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "praiseCount",value = "点赞数量",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "commentNum",value = "评论数",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "sort",value = "排序值",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "articleContent",value = "文章内容表",paramType = "query"),
            @ApiImplicitParam(name = "articleId",value = "文章ID",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "content",value = "文章内容",dataType = "String",paramType = "query")})
    public ModelAndView addArticel(HttpServletRequest request,@ModelAttribute("article") EduArticle article, @ModelAttribute("articleContent") EduArticleContent articleContent){
        ModelAndView model = new ModelAndView();
        System.out.println(article);
        System.out.println("--------------------------");
        System.out.println(articleContent);
        model.setViewName("redirect:/admin/article/showlist");
        Object uurl = request.getSession().getAttribute("uurl");
        article.setImageUrl((String) uurl);
        article.setCreateTime(new Date());
        adminArticleService.addArticle(article);
        articleContent.setArticleId(article.getArticleId());
        adminArticleService.addArticeContent(articleContent);
        return model;
    }




}
